﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QUAN_LY_MUON_TRA_SACH
{
    public partial class frmthemnxb : Form
    {
        public frmthemnxb()
        {
            InitializeComponent();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnnhaplai_Click(object sender, EventArgs e)
        {
            txtmanxb.Clear();
            txttennxb.Clear();
            //tu dong sinh ma NXB moi
            SqlCommand lenh = new SqlCommand("select max(cast(substring(ma_nxb,4,len(ma_nxb)-3) as int))+1 from nha_xuat_ban", Connection.sqlConnection);
            if (Convert.ToInt32(lenh.ExecuteScalar()) < 100)
                txtmanxb.Text = "NXB0" + Convert.ToString(lenh.ExecuteScalar());
            else if (Convert.ToInt32(lenh.ExecuteScalar()) >= 100)
                txtmanxb.Text = "NXB" + Convert.ToString(lenh.ExecuteScalar());
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand lenh = new SqlCommand("insert into nha_xuat_ban values('" + txtmanxb.Text + "'" + "," + "N'" + txttennxb.Text + "')" , Connection.sqlConnection);
                int n = lenh.ExecuteNonQuery();
                MessageBox.Show(n + " nhà xuất bản mới đã được thêm!", "Cập nhật thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                lenh.Dispose();
                FORM_MAIN.frmmain.button14_Click(sender, e);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }

        private void frmthemnxb_Load(object sender, EventArgs e)
        {
            //tu dong sinh ma NXB moi
            SqlCommand lenh = new SqlCommand("select max(cast(substring(ma_nxb,4,len(ma_nxb)-3) as int))+1 from nha_xuat_ban", Connection.sqlConnection);
            if (Convert.ToInt32(lenh.ExecuteScalar()) < 100)
                txtmanxb.Text = "NXB0" + Convert.ToString(lenh.ExecuteScalar());
            else if (Convert.ToInt32(lenh.ExecuteScalar()) >= 100)
                txtmanxb.Text = "NXB" + Convert.ToString(lenh.ExecuteScalar());
        }
    }
}
